using System;
using System.Collections;
using UnityEngine;

public enum SpawnModes // 生成模式枚举
{
    Fixed, // 固定生成
    Random // 随机生成
}

public class Spawner : MonoBehaviour // 生成器类
{
    public static Action OnWaveCompleted;

    [SerializeField, Header("生成模式")]
    private SpawnModes spawnMode = SpawnModes.Fixed;
    [SerializeField, Header("敌人数量")]
    private int enemyCount = 10;
    [SerializeField, Header("下一波敌人生成间隔")]
    private float delayBtwWaves = 1f;
    [SerializeField, Header("生成间隔")]
    private float delayBtwSpawns;
    [SerializeField, Header("随机生成的最小延迟")]
    private float minRandomDelay;
    [SerializeField, Header("随机生成的最大延迟")]
    private float maxRandomDelay;

    private float _spawnTimer; // 生成计时器
    private int _enemiesSpawned; // 已生成敌人数量
    private int _enemiesRamaining;

    // private ObjectPooler _pooler;

    private Waypoint _waypoint;

    [Header("Poolers")]
    [SerializeField] private ObjectPooler enemyWave10Pooler;
    [SerializeField] private ObjectPooler enemyWave11To20Pooler;
    [SerializeField] private ObjectPooler enemyWave21To30Pooler;


    private void Start()
    {
        // _pooler = GetComponent<ObjectPooler>();
        _waypoint = GetComponent<Waypoint>();
        _enemiesRamaining = enemyCount;
    }

    private ObjectPooler GetPooler()
    {
        int currentWave = LevelManager.Instance.CurrentWave;
        if (currentWave <= 10)
            return enemyWave10Pooler;
        if (currentWave > 10 & currentWave <= 20)
            return enemyWave11To20Pooler;
        if (currentWave > 20 & currentWave <= 30)
            return enemyWave21To30Pooler;
        return null;
    }

    private void Update() // 每帧更新
    {
        _spawnTimer -= Time.deltaTime; // 更新生成计时器

        if (_spawnTimer <= 0) // 如果生成计时器小于等于0
        {
            _spawnTimer = GetSpawnDelay(); // 根据生成模式获取生成延迟

            if (_enemiesSpawned < enemyCount) // 如果已生成的敌人数量小于总数
            {
                _enemiesSpawned++; // 增加已生成敌人数量
                SpawnEnemy(); // 生成敌人
            }
        }
    }

    private void SpawnEnemy() // 生成敌人方法
    {
        // GameObject newInstance = _pooler.GetInstanceFromPool();
        GameObject newInstance = GetPooler().GetInstanceFromPool();

        Enemy enemy = newInstance.GetComponent<Enemy>();
        enemy.waypoint = _waypoint;
        enemy.ResetEnemy();
        enemy.transform.localPosition = transform.position;
        newInstance.SetActive(true);
    }

    private float GetRandomDelay() // 获取随机生成延迟的方法
    {
        float randomTimer = UnityEngine.Random.Range(minRandomDelay, maxRandomDelay); // 在最小和最大延迟之间获取一个随机值
        return randomTimer; // 返回随机生成的延迟时间
    }

    private float GetSpawnDelay()
    {
        float delay = 0f;
        if (spawnMode == SpawnModes.Fixed)
        {
            delay = delayBtwSpawns;// 如果生成模式为固定生成，则使用固定的生成间隔
        }
        else
        {
            delay = GetRandomDelay();// 如果生成模式为随机生成，则使用随机的生成间隔
        }
        return delay;
    }

    //生成下一波敌人
    private IEnumerator NextWave()
    {
        yield return new WaitForSeconds(delayBtwWaves);
        _enemiesRamaining = enemyCount;
        _spawnTimer = 0f;
        _enemiesSpawned = 0;
    }

    private void RecordEnemy(Enemy enemy)
    {
        _enemiesRamaining--;
        if (_enemiesRamaining <= 0)
        {
            OnWaveCompleted?.Invoke();
            StartCoroutine(NextWave());
        }
    }

    private void OnEnable()
    {
        Enemy.OnEndReached += RecordEnemy;
        EnemyHealth.OnEnemyKilled += RecordEnemy;
    }

    private void OnDisable()
    {
        Enemy.OnEndReached -= RecordEnemy;
        EnemyHealth.OnEnemyKilled -= RecordEnemy;
    }
}